package com.sxfq.loansupermarket.backsystem.modular.marketing.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.DateUtil;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.marketing.dao.ReservationsDao;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.IReservationsService;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.ReturnVisitStatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zx
 * @Date
 */
@Controller
@RequestMapping("/returnVisitStatistics")
public class ReturnVisitStatisticsController extends BaseController {

    @Autowired
    private ReturnVisitStatisticsService service;

    private String PREFIX = "/marketing/reservations/";

    @Autowired
    private SqlMapper sqlMapper;

    /**
     * 回访统计
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object reservationsRecord(SearchReturnVisitStatistics info, HttpServletRequest request) {
        Page<ReturnVisitStatistics> page = new PageFactory<ReturnVisitStatistics>().defaultPage();
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return REDIRECT + "/login";
        }
        try {
//            reservationsService.updateOneChannle();
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<ReturnVisitStatistics> list = service.selectPageListByStatusMap(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.selectPageListByStatusMap(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }


    /**
     * 统计查询
     */
    @RequestMapping(value = "/tongji", method = RequestMethod.GET)
    @ResponseBody
    public int change(String param1, String param2, String param3, String param4, String param5, String param6, String param7, String param8) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(param1)) {
            params.put("outpatientName", params);
        }
        if (StringUtils.isNotBlank(param2)) {
            params.put("accessDisease", param2);
        }
        if (StringUtils.isNotBlank(param3)) {
            String fieldTc = "";
            String fieldTcs[] = param3.split(",");
            if (fieldTcs.length == 1) {
                fieldTc = fieldTcs[0];
            } else {
                for (int i = 0; i < fieldTcs.length; i++) {
                    if (i == (fieldTcs.length - 1)) {
                        fieldTc += "'" + fieldTcs[i];
                    } else if (i == 0) {
                        fieldTc += fieldTcs[i] + "'" + ",";
                    } else {
                        fieldTc += "'" + fieldTcs[i] + "'" + ",";
                    }
                }
            }
            params.put("fieldTc",  "('"+fieldTc+"')");
        }
        if (StringUtils.isNotBlank(param4)) {
            if (!"请选择".equals(param4)) {
                params.put("revisit", param4);
            }
        }
        if (StringUtils.isNotBlank(param5)) {
            if (!"请选择".equals(param5)) {
                params.put("startFilingTime", param5);
            }
        }
        if (StringUtils.isNotBlank(param6)) {
            if (!"请选择".equals(param6)) {
                params.put("endFilingTime", param6);
            }
        }
        if (StringUtils.isNotBlank(param7)) {
            params.put("startRevisitTime", param7);
        }
        if (StringUtils.isNotBlank(param8)) {
            params.put("endRevisitTime", param8);
        }
        return service.getCountByMap(params);
    }

}

